﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PBDesk.Utilities;
using System.Collections;
using PBDesk.DAL;
using System.Data;
using System.Diagnostics;

namespace PBDesk.Business.Gigya
{
    public class DBMapper
    {

        internal static int InsertOrUpdateGigyaUser(GigyaUser gUser)
        {
            int result = 0;
            if (gUser != null & gUser.UID.IsNotNullAndNotEmpty())
            {
                Trace.WriteLine("PBDesk.Business.Gigya.DBMapper.InsertOrUpdateGigyaUser");
                string spName = "PBDsk_Upsert_GigyaUser";
                int dd =1,mm=1,yy=1901;
                if (gUser.birthDay.IsNotNullAndNotEmpty())
                    dd = int.Parse(gUser.birthDay);
                if (gUser.birthMonth.IsNotNullAndNotEmpty())
                    mm = int.Parse(gUser.birthMonth);
                if (gUser.birthYear.IsNotNullAndNotEmpty())
                    yy = int.Parse(gUser.birthYear);

                DateTime bdate = new DateTime(yy, mm, dd);

                string gender = "n";
                if (gUser.gender.IsNotNullAndNotEmpty())
                {
                    gender = gUser.gender.Trim().ToLower();
                    if (gender != "m" || gender != "f")
                        gender = "n";
                }

                Hashtable parameters = new Hashtable()
                {
                    {"pGigyaUID", gUser.UID},
                    {"pLoginProvider", gUser.loginProvider},
                    {"pLoginProviderUID", gUser.loginProviderUID},
                    {"pNickName", gUser.nickname},
                    {"pEmail", gUser.email},
                    {"pFName", gUser.firstName},
                    {"pLName", gUser.lastName},
                    {"pGender", gender},
                    {"pDateOfBirth", bdate},
                    {"pCity", gUser.city},
                    {"pState", gUser.state},
                    {"pCountry", gUser.country},
                    {"pZIP", gUser.zip},
                    {"pPhotoUrl", gUser.photoURL},
                    {"pThumbnailUrl", gUser.thumbnailURL},
                    {"pProfileUrl", gUser.profileURL},
                    {"pIsActive", true}
                };

                DBManager.ExecuteNonQuery(spName, parameters);
                result = 1;
            }
            return result;
        }

        internal static string IsGigyaUserRegistered(string gigyaUID)
        {
            Trace.WriteLine("PBDesk.Business.Gigya.DBMapper.IsGigyaUserRegistered() Method call start for GigyaUID " + gigyaUID);
            string spName = "PBDsk_GetUserNameFromGigyaMapper";
            string email = string.Empty;
            IDataReader reader = null ;
            try
            {
                if (gigyaUID.IsNotNullAndNotEmpty())
                {
                    Hashtable parameters = new Hashtable() { { "pGigyaUID", gigyaUID } };
                    reader = DBManager.ExecuteDataReader(spName, parameters);
                    if (reader != null)
                    {
                        if (reader.Read())
                            email = (string)DBHelper.GetValueFromDataReader<string>(reader, "CMSEmail");
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error while reading reader for " + spName, ex);
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }
            return email;
        }

        internal static bool RegisterGigyaUser(string gigyaUid, string email)
        {
            bool result = false;
            string spName = "PBDsk_Upsert_GigyaMapper";
            Hashtable parameters = new Hashtable() { { "pGigyaUID", gigyaUid }, { "pCMSEmail", email }, {"pIsActive", true} };            
            try
            {
                DBManager.ExecuteNonQuery(spName, parameters);
                result = true;
            }
            catch (Exception ex)
            {
                throw new Exception("Error while Registering Gigya User at RegisterGigyaUser(). SP: " + spName, ex);
            }
            return result;
        }
    }
}
